//
//  AXFNavView.m
//  爱鲜蜂
//
//  Created by HH on 16/11/18.
//  Copyright © 2016年 胡浩. All rights reserved.
//

#import "AXFNavView.h"
#import <CoreLocation/CoreLocation.h>

@interface AXFNavView ()<CLLocationManagerDelegate>

@property (nonatomic,weak)UIView *containerView;

@property (nonatomic,weak)UIButton *titleBtn;

@property (nonatomic,weak)UIButton *scancodeBtn;

@property (nonatomic,weak)UIButton *searchBtn;

@property (nonatomic,weak)UIView *titleView;

@property (nonatomic,weak)UILabel *titleLabel;

@property (nonatomic,strong)CLLocationManager *manager;

@end

@implementation AXFNavView

- (void)setupUI
{
    self.backgroundColor = [UIColor clearColor];
    
    UIView *containerView = [[UIView alloc] init];
    
    self.containerView = containerView;
    
    containerView.backgroundColor = [UIColor cz_colorWithHex:0xffd600];
    
    [self addSubview:containerView];
    
    UIView *titleView = [[UIView alloc] init];
    
    self.titleView = titleView;
    
    [self insertSubview:titleView aboveSubview:containerView];
    
    UILabel *titleLabel = [UILabel cz_labelWithText:@"配送至:" fontSize:10 color:[UIColor whiteColor]];
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = titleLabel;
    
    titleView.layer.cornerRadius = 10.0;
    
    titleView.clipsToBounds = YES;
    
    [titleView addSubview:titleLabel];
    
    //titleBtn
    UIButton *titleBtn = [UIButton cz_textButton:@"点击此处定位" fontSize:8 normalColor:[UIColor whiteColor] selectedColor:[UIColor whiteColor]];
    
    titleBtn.layer.borderColor = [UIColor clearColor].CGColor;
    
    [titleBtn addTarget:self action:@selector(clickTitleBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.titleBtn = titleBtn;
    
    titleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleBtn.layer.borderWidth = 0.5;
    
    titleBtn.layer.cornerRadius = 10.0;
    
    titleBtn.clipsToBounds = YES;
    
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:13 weight:3];
    
    titleBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    
    [titleView addSubview:titleBtn];
    
    //搜索
    UIButton *searchBtn = [[UIButton alloc] init];
    
    self.searchBtn = searchBtn;
    
    searchBtn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    searchBtn.layer.cornerRadius = 15.0;
    
    searchBtn.clipsToBounds = YES;
    
    [searchBtn addTarget:self action:@selector(clickSearchBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self insertSubview:searchBtn aboveSubview:containerView];
    
    //扫一扫
    UIButton *scancodeBtn = [[UIButton alloc] init];
    
    self.scancodeBtn = scancodeBtn;
    
    [scancodeBtn addTarget:self action:@selector(clickScancodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    scancodeBtn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    scancodeBtn.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    
    scancodeBtn.layer.cornerRadius = 15.0;
    
    scancodeBtn.clipsToBounds = YES;
    
    [self addSubview:scancodeBtn];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(165, 30));
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.left.offset(5);
    }];
    
    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right);
        make.centerY.equalTo(titleView);
    }];
    
//    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self).offset(8);
//        make.centerX.equalTo(self);
//        make.size.mas_equalTo(CGSizeMake(160, 30));
//    }];
    
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.right.offset(-8);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [scancodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.left.offset(8);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

- (CLLocationManager *)manager
{
    if (!_manager) {
        
        _manager = [[CLLocationManager alloc] init];
        _manager.delegate = self;
        [_manager requestAlwaysAuthorization];
        
    }
    
    return _manager;
}

- (void)clickScancodeBtn:(UIButton *)sender
{
    if ([_delegate respondsToSelector:@selector(pushToScancodeController)]) {
        [_delegate pushToScancodeController];
    }
}

#pragma mark -定位
- (void)clickTitleBtn:(UIButton *)sender
{
    [self.manager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = locations.lastObject;
    //        NSLog(@"%f===%f",location.coordinate.latitude,location.coordinate.longitude);
    
    // 地理编码
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placemark = placemarks.lastObject;
        
        if (placemark.name) {
            
            [[NSUserDefaults standardUserDefaults] setObject:placemark.name forKey:@"locationName"];
            
            [self.titleBtn setTitle:placemark.name forState:UIControlStateNormal];
            
        }
        
    }];
    [manager stopUpdatingLocation];
}

- (void)setLocationName:(NSString *)locationName
{
    _locationName = locationName;
    
    [self.titleBtn setTitle:locationName forState:UIControlStateNormal];
}


#pragma mark - 搜索
- (void)clickSearchBtn:(UIButton *)sender
{
//    NSLog(@"搜索");
    
    if ([_delegate respondsToSelector:@selector(presentSearchViewController)]) {
        
        [_delegate presentSearchViewController];
    }
}


#pragma mark - 调整颜色,图片,透明度
- (void)setContainerViewAlpha:(CGFloat)alpha
{
    
    self.containerView.alpha = alpha;

    UIColor *titleColor = alpha > 1 ? [UIColor blackColor] : [UIColor whiteColor];
    
    [self.titleBtn setTitleColor:titleColor forState:UIControlStateNormal];
    
    UIImage *scancodeImage = alpha > 1 ? [UIImage imageNamed:@"icon_black_scancode"] : [UIImage imageNamed:@"icon_white_scancode"];
    
    [self.scancodeBtn setImage:scancodeImage forState:UIControlStateNormal];
    
    UIImage *searchImage = alpha > 1 ? [UIImage imageNamed:@"icon_search"] : [UIImage imageNamed:@"icon_search_white"];
    
    self.scancodeBtn.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    [self.searchBtn setImage:searchImage forState:UIControlStateNormal];
    
    self.searchBtn.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    self.alpha = alpha < 0 ? 0.5 + alpha : 1;
  
    self.titleView.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    self.titleLabel.textColor = alpha > 1 ? [UIColor blackColor] : [UIColor whiteColor];
    
    
}

@end
